package com.ytkj.digitalConstruction.controller.constructionprocess;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ytkj.digitalConstruction.ReturnCode;
import com.ytkj.digitalConstruction.entity.constructionplan.ConstructionContent;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionEvent;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionPreparation;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.reqbean.PlanAddParam;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionApproachService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionBaseService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionConcentrateNoticeService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionConcentrateService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionPlanService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionPreparationService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionReviewService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionUtilService;
import com.ytkj.digitalConstruction.service.constructionprocess.ContructionEmergencyEventsService;

/**
 * 不停航施工流程操作Controller
 * @author 宁煜
 *
 */
@RestController
@RequestMapping("constructionoperation")
public class ConstructionOperationController {

	@Autowired
	private ConstructionConcentrateService constructionProcessService;
	
	@Autowired
	private ConstructionConcentrateNoticeService constructionConcentrateNoticeService;
	
	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionPreparationService constructionPreparationService;
	
	@Autowired
	private ConstructionPlanService constructionPlanService;
	
	@Autowired
	private ConstructionReviewService constructionReviewService;

	@Autowired
	private ContructionEmergencyEventsService contructionEmergencyEventsService;
	
	@Autowired
	private ConstructionApproachService constructionApproachService;
		
	private Logger log = LoggerFactory.getLogger(ConstructionOperationController.class);
	
	/**
	 * @Description: 填写进场时间和进场路线
	 * @Title: setPreparation 
	 * @param @param constructionPreparation
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@PostMapping("preparation/setting")
	public ReturnCode setPreparation(@RequestBody @Valid ConstructionPreparation constructionPreparation, 
			BindingResult bindingResult, HttpServletRequest request){
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("设置风险出现错误，原因为："+msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		
		WholeFlowInfo wholeFlowInfo = constructionPreparationService.setOrUpdatePreparation(constructionPreparation, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 设置/修改施工计划
	 * @Title: setPlan 
	 * @param @param planAddParam
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@PostMapping("plan/setting")
	public ReturnCode setPlan(@RequestBody @Valid PlanAddParam planAddParam, BindingResult bindingResult, HttpServletRequest request){
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("设置施工计划出错，原因："+msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		
		List<ConstructionContent> constructionContent = planAddParam.getConstructionContent();
		for(ConstructionContent item : constructionContent) {
			if(item.getContent().length() > 500 || item.getInitialContent().length() > 500){
				log.error("施工计划过长！");
				return new ReturnCode.Builder().Success(false).Msg("施工计划过长！").build();
			}
		}
		WholeFlowInfo wholeFlowInfo = constructionPlanService.setOrUpdatePlan(planAddParam, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 监理确认施工计划
	 * @Title: surePlanSupervisor 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("plan/supervisor/confirmation/{flowId}")
	public ReturnCode confirmPlanSupervisor(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionPlanService.confirmPlanSupervisor(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 项目部确认收到通知
	 * @Title: confirmPlanOperating 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("project/confirmation/{flowId}")
	public ReturnCode confirmProject(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo =constructionConcentrateNoticeService.confirmProject(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 设置集结信息
	 * @Title: setConcentrate 
	 * @param @param constructionPreparation
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("concentrate/setting/{flowId}")
	public ReturnCode setConcentrate(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionProcessService.setConcentrate(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}

	/**
	 * @Description: 设置摆放标识
	 * @Title: setApproachPutMark 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("approach/putmark/setting/{flowId}")
	public ReturnCode setApproachPutMark(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionApproachService.setApproachPutMark(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 指挥部确认申请进入施工区
	 * @Title: setPlan 
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("approach/confirmation/{flowId}")
	public ReturnCode confirmApproach(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionApproachService.confirmApproach(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 进入施工区之前安检确认
	 * @Title: confirmEnterConstruction 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("enterconstruction/confirmation/{flowId}")
	public ReturnCode confirmEnterConstruction(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionApproachService.confirmEnterConstruction(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 施工结束
	 * @Title: endConstruction 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("construction/end/{flowId}")
	public ReturnCode endConstruction(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionBaseService.endConstruction(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 手动施工结束
	 * @Title: endConstructionManual 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("construction/manualend/{flowId}")
	public ReturnCode endConstructionManual(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionBaseService.endConstructionManual(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 填写应急事件
	 * @Title: setEmergencyEvents 
	 * @param @param constructionEvent
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@PostMapping("emergencyevents/setting")
	public ReturnCode setEmergencyEvents(@RequestBody @Valid ConstructionEvent constructionEvent, BindingResult bindingResult, HttpServletRequest request){
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("设置应急事件失败，原因："+msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		WholeFlowInfo wholeFlowInfo = contructionEmergencyEventsService.setEmergencyEvents(constructionEvent, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 复查建监理确认（施工结束后，必然执行）
	 * @Title: setReview 
	 * @param @param constructionReview
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("review/setting/{flowId}")
	public ReturnCode setReview(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionReviewService.setReview(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 复查时的安检确认
	 * @Title: confirmReview 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("review/confirmation/{flowId}")
	public ReturnCode confirmReview(@PathVariable("flowId") Integer flowId, HttpServletRequest request){
		WholeFlowInfo wholeFlowInfo = constructionReviewService.confirmReview(flowId, ConstructionUtilService.getUserByRequest(request));
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}

	/**
	 * @Description: 获取指定项目昨日施工计划中的施工内容
	 * @Title: getTodaysContent 
	 * @param @param projectId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("todaycontent/show/{projectId}")
	public ReturnCode getTodaysContent(@PathVariable("projectId") Integer projectId) {
		return new ReturnCode.Builder()
				.Object(constructionPlanService.getTodaysContent(projectId))
				.build();
	}
}
